#include <QtGui>

#include "batt-main.h"
#include "common-batt.h"
#include "stdio.h"
#ifdef __cplusplus
extern "C" {
#endif

#define DEVICE "/sys/class/power_supply/ds2782-0/"
char *env_device = DEVICE;
void DlgShutdown::geometry()
{
	int xres;
	int yres;

	// 自适应屏幕
	QDesktopWidget *desktopWidget = QApplication::desktop();
	QRect           screenRect    = desktopWidget->screenGeometry();
	xres = screenRect.width() ;
	yres = screenRect.height();
	setGeometry(0, 0, 90, 20);

	// 窗口置顶
	setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
	
	this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
	// 方法1 透明
	QPalette pal = palette();
	setPalette(pal);
	pal.setColor(QPalette::Background, QColor(0xff, 0xff, 0xff, 0x00));

	// 方法1 透明百分比
	this->setWindowOpacity(0.7);
	setCursor(Qt::BlankCursor);
	// 方法2 背景完全透明，控件按照百分比
	// this->setAttribute(Qt::WA_TranslucentBackground, true);
	// setAttribute(Qt::WA_ShowWithoutActivating, false);
}

DlgShutdown::DlgShutdown(QWidget *parent)
	: QDialog(parent)
{
	geometry();

	QHBoxLayout *layout = new QHBoxLayout();

	// QToolButton *pButton0 = new QToolButton();
	pLabel0 = new QLabel();
	pLabel0->setText("Volt");

	pLabel1 = new QLabel();
	pLabel1->setText("Cap");

	pLabel2 = new QLabel();
	pLabel2->setText("Cur");

	pLabel3 = new QLabel();
	pLabel3->setText("tmep");
	// QLabel *pButton1 = new QLabel();

	layout->addWidget(pLabel0, 0);
	layout->addWidget(pLabel1, 1);
	layout->addWidget(pLabel2, 2);
	layout->addWidget(pLabel3, 3);


	this->setStyleSheet("QToolButton{border:none;}"
	                    "QToolButton:focus{background: rgb(191,191,191);}"
	                   );


	setLayout(layout);
	// pButton0->FocusPolicy(Qt::NoFocus);
	
	// SDThread *thread = new SDThread;
	// connect(pButton0, SIGNAL(clicked()), this, SLOT(on_cancel()));
	// connect(pButton1, SIGNAL(clicked()), this, SLOT(on_shutdown()));
	// connect(pButton1, SIGNAL(clicked()), this, SLOT(on_shutdown()));
	// connect(thread, SIGNAL(sig_show()),this, SLOT(on_slot_show()));
	// thread->start();

	char *env = NULL;
	env = getenv("DEVICE_BATT");
    if (env) {
        env_device = env;
    }
	startTimer(1000);
	return ;
}

void DlgShutdown::timerEvent(QTimerEvent *t)
{
	
	FILE *fd;
	char strout[32], strfname[256];
	int num;
	

	snprintf(strfname, sizeof(strfname), "%s/capacity", env_device);
	fd = fopen(strfname, "r");
	fscanf(fd, "%d", &num);	
	snprintf(strout, sizeof(strout), "%d%%", num);
	fclose(fd);
	pLabel0->setText(strout);
	

	snprintf(strfname, sizeof(strfname), "%s/current_now", env_device);
	fd = fopen(strfname, "r");
	fscanf(fd, "%d", &num);	
	snprintf(strout, sizeof(strout), "%dmA", num / 1000);
	fclose(fd);
	pLabel1->setText(strout);

	snprintf(strfname, sizeof(strfname), "%s/voltage_now", env_device);
	fd = fopen(strfname, "r");
	fscanf(fd, "%d", &num);	
	snprintf(strout, sizeof(strout), "%dmV", num);
	fclose(fd);
	pLabel2->setText(strout);

	snprintf(strfname, sizeof(strfname), "%s/temp", env_device);
	fd = fopen(strfname, "r");
	fscanf(fd, "%d", &num);	
	snprintf(strout, sizeof(strout), "%dc", num);
	fclose(fd);
	pLabel3->setText(strout);

	// fd = fopen(DEVICE "/current_now", "r");
	// fgets(buf, sizeof(buf), fd);
	// fclose(fd);
	// pLabel1->setText(tr("%1mA").arg(buf));

	// fd = fopen(DEVICE "/current_now", "r");
	// fgets(buf, sizeof(buf), fd);
	// fclose(fd);
	// pLabel2->setText(tr("%1mA").arg(buf));

	// fd = fopen(DEVICE "/current_now", "r");
	// fgets(buf, sizeof(buf), fd);
	// fclose(fd);
	// pLabel3->setText(tr("%1mA").arg(buf));

	
}
// void DlgShutdown::on_slot_show()
// {
// 	// printf("on show()\r\n");
// 	show();
// }
// void DlgShutdown::on_shutdown()
// {
// 	// system("echo 1 > /sys/class/backlight/mxs-bl/bl_power");
// 	// system("echo 0 > /sys/class/backlight/mxs-bl/brightness");
// 	// system("reboot");
// }

// void DlgShutdown::on_cancel()
// {
// 	// this->hide();
// }

void DlgShutdown::keyPressEvent(QKeyEvent *event)
{
	switch(event->key()) {
	case Qt::Key_Escape:
	// 	hide();
		// event->ignore();
		break;

	default:
		QDialog::keyPressEvent(event);
	}
}
// void DlgShutdown::mousePressEvent(QMouseEvent *event)
// {
// 	// printf("release");
// 	// hide();
// }
// void DlgShutdown::focusInEvent(QFocusEvent *event)
// {
// 	pButton0->setFocus();	
// }

#ifdef __cplusplus
}
#endif
